Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Angular router link tests to use RouterTestingHarness #2430

Merged
merged 14 commits into from
Oct 9, 2024

Conversation

jattasNI
Copy link
Contributor

@jattasNI jattasNI commented Oct 3, 2024

Pull Request

🤨 Rationale

We have several tests that verify Angular router link behavior by clicking links and inspecting router state. These tests exhibit a couple problems:

  1. currently they report warnings in the output log like: [web-server]: 404: /_karma_webpack_/page1?param1=true
  2. In Upgrade Playwright and Apache Arrow dependencies #2387 when we bring in a newer version of Chromium, these cause other tests to fail to execute and report timeouts

The root cause of these issues is that the tests are actually trying to navigate the page when the link is clicked.

👩‍💻 Implementation

"with-href" nimbleRouterLink tests

In researching best practices for writing tests like this I learned that the RouterTestingModule we had been using has been deprecated and replaced with a more powerful RouterTestingHarness. This blog and video does a good job of explaining it, much better than the angular docs. The basic idea is that the harness sets up a parent component and router which host your component under test. When something tries to navigate the harness captures information about the navigation but doesn't actually navigate the page.

The fixes in this PR are to use RouterTestingHarness instead of RouterTestingModule. This has these side effects:

  1. The routes are configured with provideRouter instead of withRoutes
  2. The navigated route started to be relative to the current route, so starting from /start and clicking a link to page resulted in a URL of /start/page. The simplest fix I found for this was to change the starting page to /.
  3. Some setup code could be deleted and made sync/async instead of fakeAsync.

In addition even if the router doesn't navigate the page we are still invoking click handlers on anchors which try to navigate the page. To address that I'm calling preventDefault() from those handlers.

error routerLink tests

These tests included a RouterTestingModule but didn't actually need it or RouterTestingHarness so I deleted those imports.

package.json

Added a couple dev scripts that I found useful while running tests locally. We didn't have an obvious way to debug angular tests or to run just the tests for one project and now we do.

🧪 Testing

  1. Verified the 404 warnings are no longer printed to the console
  2. Verified the tests don't navigate the page in the newer version of Chromium
  3. Verified tests still fail with various changes like changing the URL or not clicking the link

✅ Checklist

  • I have updated the project documentation to reflect my changes or determined no changes are needed.

@jattasNI
Copy link
Contributor Author

jattasNI commented Oct 3, 2024

@m-akinc I think I found a fix to the router test issues we talked about this week. Could you buddy the approach in this one test? If it looks good I'll apply it to the other router link tests.

@jattasNI jattasNI requested a review from m-akinc October 3, 2024 18:55
Copy link
Contributor

@m-akinc m-akinc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. The only thing I would suggest is that when updating the rest of the tests, there might be a way to simplify the reuse (and reduce duplication) by introducing a NimbleRouterTestingHarness that abstracts all the common configuration and stuff like:

public get anchor(): Anchor {
    return this.harness.fixture.debugElement.query(By.css('nimble-anchor')).nativeElement as Anchor;
}

@jattasNI
Copy link
Contributor Author

jattasNI commented Oct 8, 2024

Looks good to me. The only thing I would suggest is that when updating the rest of the tests, there might be a way to simplify the reuse (and reduce duplication) by introducing a NimbleRouterTestingHarness that abstracts all the common configuration and stuff like:

public get anchor(): Anchor {
    return this.harness.fixture.debugElement.query(By.css('nimble-anchor')).nativeElement as Anchor;
}

@m-akinc I've applied the pattern to all the tests and reset your vote. I considered this suggestion but didn't end up using it. For test code I'm ok with a bit more duplication in the interest of readability / transparency so it didn't seem worth it to share just this one line.

@jattasNI jattasNI requested a review from m-akinc October 8, 2024 17:22
@jattasNI jattasNI marked this pull request as ready for review October 8, 2024 19:49
packages/angular-workspace/package.json Outdated Show resolved Hide resolved
packages/angular-workspace/package.json Show resolved Hide resolved
@jattasNI jattasNI enabled auto-merge (squash) October 9, 2024 21:44
@jattasNI jattasNI merged commit 943ce38 into main Oct 9, 2024
10 of 12 checks passed
@jattasNI jattasNI deleted the router-link-tests branch October 9, 2024 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants